//==============================================================================
//
//               Copyright (C) 2003  Dick van Oudheusden
//  
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2005-01-30 08:05:48 $ $Revision: 1.4 $
//
//==============================================================================

#include "ofc/DDateTime.h"
#include "ofc/DTimer.h"

#include "DTest.h"

//-DataTypes-------------------------------------------------------------------

void DDateTime_test()
{
  DDateTime *one  = [DDateTime new];
  DDateTime *two  = [DDateTime alloc];
  DText     *txt  = nil;
  char      *pntr = NULL;  
  
  STARTTEST();

  TEST( [DDateTime isTimeValid :0  :0  :0  :0  ]);
  TEST( [DDateTime isTimeValid :23 :59 :59 :999]);
  TEST(![DDateTime isTimeValid :24 :00 :00 :0  ]);
  TEST(![DDateTime isTimeValid :23 :60 :59 :0  ]);
  TEST(![DDateTime isTimeValid :23 :59 :60 :0  ]);
  TEST(![DDateTime isTimeValid :23 :59 :59 :1000]);

  TEST( [DDateTime isDateValid :1970 :1  :1 ]);
  TEST( [DDateTime isDateValid :2004 :12 :31]);
  TEST( [DDateTime isDateValid :2004 :2  :29]);
  TEST(![DDateTime isDateValid :1969 :1  :1 ]);
  TEST(![DDateTime isDateValid :1970 :0  :1 ]);
  TEST(![DDateTime isDateValid :1970 :13 :1 ]);
  TEST(![DDateTime isDateValid :1970 :1  :0 ]);
  TEST(![DDateTime isDateValid :1970 :1  :32]);
  TEST(![DDateTime isDateValid :1970 :2  :29]);
  TEST( [DDateTime isDateValid :2000 :2  :29]);

  TEST([DDateTime isValid :2004 :11 :20 :11 :7 :50 :604]);
  
  TEST([one set :2004 :11 :20 :11 :7 :50 :604]);
  
  TEST([one year] == 2004);
  TEST([one month] == 11);
  TEST([one day] == 20);
  TEST([one hours] == 11);
  TEST([one minutes] == 7);
  TEST([one seconds] == 50);
  TEST([one millis] == 604);
  TEST([one weekday] == 6);
  
  [two init :2004 :11 :21 :8 :7 :50];

  TEST([two year] == 2004);
  TEST([two month] == 11);
  TEST([two day] == 21);
  TEST([two hours] == 8);
  TEST([two minutes] == 7);
  TEST([two seconds] == 50);
  TEST([two millis] == 0);
  TEST([two weekday] == DDT_SUNDAY);

  TEST([one compare :two] == -1);
  
  [two free];
  
  two = [one copy];
  
  TEST(one != two);
  
  TEST([one compare :two] == 0);

  TEST([two time :10 :10 :10]);
  
  TEST([one compare :two] == 1);
  
  TEST([two date :[one year] :[one month] :[one day] + 1]);
  
  TEST([one compare :two] == -1);
  
  TEST([one UTCTime]);
  
  txt = [one toText];          // printf("UTC-ISO: %s\n", [txt cstring]);
  [txt free];
  
  TEST([one localTime]);
  
  TEST([one set :2004 :11 :21 :19 :20 :20 :0]);

  // ISO test
  txt = [one toISO8601];       // printf("now-ISO: %s\n", [txt cstring]);
  
  pntr = (char *) [txt cstring]; // Don't do this at home..
  
  TEST([two fromString :&pntr] == 0);
  
  TEST([one compare :two] == 0);
  
  [txt free];
  
  // asc test
  txt = [one toASC];           // printf("ASC    : %s\n", [txt cstring]);

#ifdef HAVE_DDATETIME_PARSE
  pntr = (char *) [txt cstring]; // Don't do this at home..
  
  TEST([two fromASC :&pntr]);
  
  TEST([one compare :two] == 0);
#endif
  
  [txt free];
  
  // RFC1123
  txt = [one toRFC1123];       // printf("RFC1123: %s\n", [txt cstring]);
  
#ifdef HAVE_DDATETIME_PARSE
  pntr = (char *) [txt cstring]; // Don't do this at home..
  
  TEST([two fromRFC1123 :&pntr]);
  
  TEST([one compare :two] == 0);
#endif
  
  [txt free];
  
  // RFC850
  txt = [one toRFC850];        // printf("RFC850 : %s\n", [txt cstring]);
  
#ifdef HAVE_DDATETIME_PARSE
  pntr = (char *) [txt cstring]; // Don't do this at home..
  
  TEST([two fromRFC850 :&pntr]);
  
  TEST([one compare :two] == 0);
#endif
  
  [txt free];
  
  // RFC822
  txt = [one toRFC822];        // printf("RFC822 : %s\n", [txt cstring]);
  
#ifdef HAVE_DDATETIME_PARSE
  pntr = (char *) [txt cstring]; // Don't do this at home..
  
  if ([two fromRFC822 :&pntr]) // Not all strptime support %z
  {
    TEST([one compare :two] == 0);
  }
#endif
  
  [txt free];

  // free format
  txt = [one format :"%c"];    // printf("Pref   : %s\n", [txt cstring]);
  
#ifdef HAVE_DDATETIME_PARSE
  pntr = (char *) [txt cstring]; // Don't do this at home..
  
  TEST([two parse :&pntr :"%c"]);
  
  TEST([one compare :two] == 0);
#endif
  
  [txt free];

  
  STOPTEST();

  [one free];
  [two free];
}

